package l2ft.gameserver.handler.admincommands.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import l2ft.gameserver.handler.admincommands.IAdminCommandHandler;
import l2ft.gameserver.hwid.HwidEngine;
import l2ft.gameserver.hwid.HwidGamer;
import l2ft.gameserver.listener.actor.player.OnAnswerListener;
import l2ft.gameserver.model.Player;
import l2ft.gameserver.model.entity.poll.Poll;
import l2ft.gameserver.model.entity.poll.PollEngine;
import l2ft.gameserver.network.l2.components.SystemMsg;
import l2ft.gameserver.network.l2.s2c.ConfirmDlg;
import l2ft.gameserver.network.l2.s2c.NpcHtmlMessage;
import l2ft.gameserver.utils.Log;

public class AdminHwid implements IAdminCommandHandler
{
	private static enum Commands
	{
		admin_character_hwid,
		admin_ban_hwid
	}

	@Override
	public boolean useAdminCommand(Enum comm, String[] wordList, String fullString, Player activeChar)
	{
		
		if(fullString.startsWith("admin_character_hwid"))
		{
			String hwid = fullString.substring("admin_character_hwid ".length());
			String page = getHwidPage(hwid);
			activeChar.sendPacket(new NpcHtmlMessage(0).setHtml(page));
		}
		else if(fullString.startsWith("admin_ban_hwid"))
		{
			if(!activeChar.getPlayerAccess().CanBan)
				return false;
			
			String hwid = fullString.substring("admin_ban_hwid ".length());
			ConfirmDlg packet = new ConfirmDlg(SystemMsg.S1, 120000).addString("Are you sure you want to Ban this HWID?!?!!?");
			activeChar.ask(packet, new AskBanHwid(activeChar, hwid));
		}
		return true;
	}
	
	private class AskBanHwid implements OnAnswerListener
	{
		private String _hwidToBan;
		private Player _admin;
		private AskBanHwid(Player admin, String hwid)
		{
			_hwidToBan = hwid;
			_admin = admin;
		}
		
		@Override
		public void sayYes()
		{
			_admin.sendMessage("Hwid Banned!");
			Log.LogHwid(HwidEngine.getInstance().getGamerByHwid(_hwidToBan), _admin.getName(), "Ban", "HWID banned by "+_admin.getName());
			HwidEngine.getInstance().banHwid(_hwidToBan);
		}

		@Override
		public void sayNo()
		{
			_admin.sendMessage("Dude... don't fuck with me!");
		}
	}
	
	
	private String getHwidPage(String hwid)
	{
		HwidGamer gamer = HwidEngine.getInstance().getGamerByHwid(hwid);
		String msg = "<html><body>";
		msg += "<table width=260><tr>";
		msg += "<td width=40><button value=\"Main\" action=\"bypass -h admin_admin\" width=40 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>";
		msg += "<td width=180><center>HWID Menu</center></td>";
		msg += "<td width=40><button value=\"Back\" action=\"bypass -h admin_show_characters 0\" width=40 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>";
		msg += "</tr></table><br>";
		msg += "<table width=260>";
		for(Player onlinePlayer : gamer.getOnlineChars())
		{
			msg += "<tr><td>Player:</td>";
			msg += "<td align=left>";
			msg += "<a action=\"bypass -h admin_character_list " + onlinePlayer.getName() + "\">" + onlinePlayer.getName() + "</a>";
			msg += "</td></tr>";
		}
		msg += "</table><br>";
		msg += "First Time Logged: "+getConvertedDateOfDeath(gamer.getFirstTimePlayed()*1000L)+"<br>";
		msg += "Total Time Played: "+getTotalTimePlayed(gamer.getTotalTimePlayed())+"<br>";
		Poll activePoll = PollEngine.getInstance().getActivePoll();
		msg += "Poll Vote: "+ (activePoll == null ? "No Active Poll" : (gamer.getPollAnswer() < 0 ? "No Answer" : activePoll.getAnswerById(gamer.getPollAnswer()).getAnswer()))+"<br>";
		msg += "<br>Ban Hwid:<br>";
		msg += "<button value=\"Ban Hwid\" action=\"bypass -h admin_ban_hwid "+gamer.getHwid()+"\" width=80 height=25 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"><br>";
		msg += "<br>Logs:";
		//TODO logs
		
		return msg;
	}
	
	private String getConvertedDateOfDeath(long time)
	{
		if(time == 0)
			return "";
		Date anotherDate = new Date(time);
		String DATE_FORMAT = "MM/dd/yy HH:mm";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US);
         
		return sdf.format(anotherDate);
	}
	
	private String getTotalTimePlayed(long seconds)
	{
		String text = "";
		System.out.println("seconds:"+seconds);
		if(seconds > 86400)
		{
			int days = (int)Math.floor((double)seconds/86400L);
			text += days + " days ";
			seconds -= days * 86400L;
		}
		
		if(seconds > 3600)
		{
			int hours = (int)Math.floor((double)seconds/3600L);
			text += hours + " hours ";
			seconds -= hours * 3600L;
		}
		
		if(seconds > 60)
		{
			int minutes = (int)Math.floor((double)seconds/60L);
			text += minutes + " minutes ";
			seconds -= minutes * 60L;
		}
		
		if(seconds > 0)
		{
			text += seconds + " seconds.";
		}
		return text;
	}

	@Override
	public Enum[] getAdminCommandEnum()
	{
		return Commands.values();
	}
}
